package com.kefu.robot.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.common.context.KefuContext;
import com.kefu.robot.mapper.RobotAidataDocTypeMapper;
import com.kefu.robot.model.RobotAidataDocType;
import com.kefu.robot.service.RobotAidataDocService;
import com.kefu.sys.mapper.SysUserMapper;
import com.kefu.sys.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.robot.model.RobotAidataDoc;
import com.kefu.robot.vo.RobotAidataDocSearchVo;
import com.kefu.robot.vo.RobotAidataDocInsertVo;
import com.kefu.robot.vo.RobotAidataDocUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiaoyu
 * @date 2023/11/03
 */
@RestController
@Api(tags = "AI模型词条")
@RequestMapping("robot/aidata/doc")
public class RobotAidataDocController extends AbstractBaseController {

    @Autowired
    private RobotAidataDocService robotAidataDocService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RobotAidataDocTypeMapper robotAidataDocTypeMapper;


    @PostMapping("/add")
    @ApiOperation(value = "新增AI模型词条")
    public Result<String> add(@Validated @RequestBody RobotAidataDocInsertVo robotAidataDocVo) throws Exception{
        logger.info("新增AI模型词条:{}", robotAidataDocVo);

        robotAidataDocService.saveAidataDoc(robotAidataDocVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除AI模型词条",notes = "支持批量删除，多个id以英文逗号分割")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除AI模型词条:{}", delete);

        robotAidataDocService.removeAidataDoc(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改AI模型词条")
    public Result<String> update(@Validated @RequestBody RobotAidataDocUpdateVo robotAidataDocVo) throws Exception{
        logger.info("修改AI模型词条:{}", robotAidataDocVo);

        robotAidataDocService.updateAidataDoc(robotAidataDocVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "AI模型词条详情")
    public Result<RobotAidataDoc> detail(@Validated @RequestBody DetailVo detail) {

        RobotAidataDoc robotAidataDoc = robotAidataDocService.getById(detail.getId());
        return ResultGenerator.genSuccessResult(robotAidataDoc);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询AI模型词条")
    public Result<Page<RobotAidataDoc>> list(@Validated @RequestBody RobotAidataDocSearchVo search) {

        Page<RobotAidataDoc> page = robotAidataDocService.searchLambdaPage(search);

        Map<String, String> typeMap = robotAidataDocTypeMapper.selectList(Wrappers.lambdaQuery(RobotAidataDocType.class)
                .eq(RobotAidataDocType::getAidataId, search.getAidataId())
                .eq(RobotAidataDocType::getTenantId, KefuContext.getKefuContext().getTenantId())).stream().filter(type ->
                StringUtils.isNotBlank(type.getDocTypeId()) && StringUtils.isNotBlank(type.getName()))
                .collect(Collectors.toMap(RobotAidataDocType::getDocTypeId, RobotAidataDocType::getName));

        Map<String, String> userMap = sysUserMapper.selectList(Wrappers.lambdaQuery(SysUser.class)
                        .eq(SysUser::getTenantId, KefuContext.getKefuContext().getTenantId()))
                .stream().filter(user -> StringUtils.isNotBlank(user.getUserName()) && StringUtils.isNotBlank(user.getUserId()))
                .collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        page.getRecords().forEach(doc -> {
            doc.setCreateUserName(userMap.get(doc.getCreateUser()));
            doc.setUpdateUserName(userMap.get(doc.getUpdateUser()));
            doc.setDocTypeName(typeMap.get(doc.getDocTypeId()));
        });
        return ResultGenerator.genSuccessResult(page);
    }
}
